package com.ruoyi.archive.service.impl;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.archive.domain.SRetiredNoMilitary;
import com.ruoyi.archive.domain.SSoldierBaseInfo;
import com.ruoyi.archive.service.ISTransferredCadresService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.archive.mapper.SRetiredRedArmyMapper;
import com.ruoyi.archive.domain.SRetiredRedArmy;
import com.ruoyi.archive.service.ISRetiredRedArmyService;

/**
 * 退伍红军老战士信息Service业务层处理
 *
 * @author Allen
 * @date 2021-09-29
 */
@Service
public class SRetiredRedArmyServiceImpl extends ServiceImpl<SRetiredRedArmyMapper, SRetiredRedArmy> implements ISRetiredRedArmyService {
    @Autowired
    private SRetiredRedArmyMapper sRetiredRedArmyMapper;

    @Autowired
    private ISTransferredCadresService transferredCadresService;

    /**
     * 查询退伍红军老战士信息
     *
     * @param id
     *         退伍红军老战士信息主键
     *
     * @return 退伍红军老战士信息
     */
    @Override
    public SRetiredRedArmy selectSRetiredRedArmyById(Long id) {
        return sRetiredRedArmyMapper.selectSRetiredRedArmyById(id);
    }

    /**
     * 查询退伍红军老战士信息列表
     *
     * @param sRetiredRedArmy
     *         退伍红军老战士信息
     *
     * @return 退伍红军老战士信息
     */
    @Override
    public List<SRetiredRedArmy> selectSRetiredRedArmyList(SRetiredRedArmy sRetiredRedArmy) {
        List<SRetiredRedArmy> retiredRedArmyList = sRetiredRedArmyMapper.selectSRetiredRedArmyList(sRetiredRedArmy);
        if (CollectionUtil.isNotEmpty(retiredRedArmyList)) {
            List<Long> soldierIdList = retiredRedArmyList.stream().map(SRetiredRedArmy::getSoldierId).collect(Collectors.toList());
            Map<Long, SSoldierBaseInfo> baseInfoMap = transferredCadresService.getSoldierBaseInfoMap(soldierIdList);
            retiredRedArmyList.stream().forEach(x -> x.setSoldierName(baseInfoMap.get(x.getSoldierId()).getName()));
            retiredRedArmyList.stream().forEach(x -> x.setIdCard(baseInfoMap.get(x.getSoldierId()).getIdCard()));
        }
        return retiredRedArmyList;
    }

    /**
     * 新增退伍红军老战士信息
     *
     * @param sRetiredRedArmy
     *         退伍红军老战士信息
     *
     * @return 结果
     */
    @Override
    public int insertSRetiredRedArmy(SRetiredRedArmy sRetiredRedArmy) {
        if (sRetiredRedArmy != null) {
            return sRetiredRedArmyMapper.insertSRetiredRedArmy(sRetiredRedArmy);
        }
        return 0;
    }

    /**
     * 修改退伍红军老战士信息
     *
     * @param sRetiredRedArmy
     *         退伍红军老战士信息
     *
     * @return 结果
     */
    @Override
    public int updateSRetiredRedArmy(SRetiredRedArmy sRetiredRedArmy) {
        return sRetiredRedArmyMapper.updateSRetiredRedArmy(sRetiredRedArmy);
    }

    /**
     * 批量删除退伍红军老战士信息
     *
     * @param ids
     *         需要删除的退伍红军老战士信息主键
     *
     * @return 结果
     */
    @Override
    public int deleteSRetiredRedArmyByIds(Long[] ids) {
        return sRetiredRedArmyMapper.deleteSRetiredRedArmyByIds(ids);
    }

    /**
     * 删除退伍红军老战士信息信息
     *
     * @param id
     *         退伍红军老战士信息主键
     *
     * @return 结果
     */
    @Override
    public int deleteSRetiredRedArmyById(Long id) {
        return sRetiredRedArmyMapper.deleteSRetiredRedArmyById(id);
    }
}
